
function msg() {
    var _this = this;
    _this.page;
    _this.page_size = 10;
    _this.userId;
    _this.roleName;

    _this.init = function(page, userId, roleName){
        _this.page = page;
        _this.userId = userId;
        _this.roleName = roleName;

        //绑定事件
        _this.initEvent();

        //避免预览范围后选项不是当前过滤条件
        $('#click_msg_type').find("span[data-value=" + $("#type").val() + "]").trigger("click");

        //加载数据
        _this.initTable();
    };

    var userIdAll = [];
    var object = {};
    /**
     * 全选事件
     */
    $("#checkAll").click(function () {
            if (this.checked) {
                $("#msg_table :checkbox:not(:disabled)").prop("checked", true);

                $("#msg_table :checkbox:not(:disabled)").each(function () {
                    if (!object[$(this).val()]) {
                        object[$(this).val()] = 1;
                        userIdAll.push($(this).val());
                    }
                });
            } else {
                $("#msg_table :checkbox:not(:disabled)").prop("checked", false);
                if (userIdAll.length > 0) {
                    for (var a = 0; a < userIdAll.length; a++) {
                        $("#msg_table :checkbox:not(:disabled)").each(function () {
                            if ($(this).val() === userIdAll[a]) {
                                delete object[$(this).val()];
                                userIdAll.splice(a, 1);
                            }
                        })
                    }
                }
            }
          //  console.log(userIdAll);
        }
    );

    /**
     * 单选事件
     */
    $("#msg_table").on("click", "input[type=checkbox]", function () {
        if ($(this).is(":checked")) {
            if (!object[$(this).val()]) {
                object[$(this).val()] = 1;
                userIdAll.push($(this).val());
            }
        } else {
            if (userIdAll.length > 0) {
                for (var a = 0; a < userIdAll.length; a++) {
                    if ($(this).val() === userIdAll[a]) {
                        delete object[$(this).val()];
                        userIdAll.splice(a, 1);
                    }
                }
            }
        }
        //console.log(userIdAll);
    });
    /**
     *
     * 清空所有的选择
     */
    $("#clearSelAll").on("click", function () {
        object={};
        userIdAll = [];
        $("#msg_table :checkbox:not(:disabled)").each(function () {
            $(this).prop("checked",false);
        })

    });


    _this.initTable = function(){
        var list_url = appPath + "/popup/msg/list";
        _this.page.init("msg_form", list_url, "msg_table", "msg_page", 1, _this.page_size);
        _this.page.goPage(1);
        _this.page.list = function(dataList){
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr>';
                inner += '<td width="50">' + (i+1) + '</td>';
                //只有管理员才有选中框
                if(_this.roleName == 'company_admin'){
                    inner += "<td width='60'>";
                    var sendId = item['sendId'];
                    if(sendId == _this.userId){
                        inner += "<input type='checkbox' value='" + item['msgId'] + "'/>";
                    }else{
                        inner += "<input type='checkbox' title='我不是你创建的!' disabled value='" + item['msgId'] + "'/>";
                    }
                    inner += "</td>";
                }
                inner += '<td class="warp" style="padding-left:5px;"><div class="pull-left content">';
                inner += '<a title="'+ item['msgTitle'] + '" onclick="msg.view(this,\'' + item['msgId'] + '\',\'' + item['status'] + '\')" href="javascript:void(0);';
                var status = item['status'];
                if(status == 2){
                    inner += '" style="font-weight: normal;color: #3e3e3e;">'+ item['msgTitle'] + '</a></div>';
                }else{
                    inner += '">'+ item['msgTitle'] + '</a></div>';
                }
                var isHot = item['isHot'];
                if(isHot == '1'){
                    inner += '<div class="pull-left hot"></div>';
                }
                inner += '</td>';
                var sendCompanyName = item['sendCompanyName'] == undefined ? "" : item['sendCompanyName'];
                var sendName = item['sendName'] == undefined ? "" : item['sendName'];
                var companyAndUser = sendCompanyName + "/" + sendName;
                inner += '<td width="256">' + companyAndUser + '</td>';
                if("" == item['publishTime']){
                    inner += '<td width="160">'+ TimeUtil.longMsTimeConvertToDateTime(item['createDate']) + '</td>';
                }else{
                    inner += '<td width="160">'+ TimeUtil.longMsTimeConvertToDateTime(item['publishTime']) + '</td>';
                }
                inner += '<td width="150">';
                inner += '<a href="javascript:msg.view(this,\'' + item['msgId'] + '\',\'' + item['status'] + '\')" class="a a-view">详情</a>';
                //管理员角色下才能删除自己创建的公告
                if(sendId == _this.userId && _this.roleName == 'company_admin'){
                    inner += '<a href="javascript:msg.delete(\'' + item['msgId'] + '\')" class="a a-close">删除</a>';
                }
                inner += '</td></tr>';
            }
            return inner;
        }
    };

    _this.view = function (t, msgId, status) {
        // $(t).parents("tr").find("a[title]").css("font-weight", "normal");
        // $(t).parents("tr").find("a[title]").css("color", "#3e3e3e");
        // window.open(appPath + '/popup/msg/view?msgId=' + msgId + "&status=" + status);
      //  window.location.href = appPath + '/popup/msg/view?msgId=' + msgId + "&status=" + status

        window.open(appPath + '/popup/msg/view?msgId=' + msgId + "&status=" + status,"_blank");
    };

    _this.delete = function (msgId) {
        var msgIds = '';
        if(msgId){
            msgIds = msgId;
        }else{
            //msgIds = _this.getCheckedRows_ids();
            msgIds = userIdAll.toString();
            alert(msgIds);

            if(userIdAll.length === 0){
                layer.msg("未勾选公告");
                return;
            }
        }
        layer.confirm("你确定删除吗?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function() {
            var url = appPath + '/popup/msg/delete';
            var params = {'msgIds': msgIds};
            var data = _this.ajax(url, params);
            var code = data['code'];
            if ("10000"==code) {
                layer.msg("删除成功");
                _this.initTable();
            }else{
                var message = data['message'];
                layer.msg(message == undefined ? "删除成功" : message);
            }
        });
    };

    _this.getCheckedRows_ids = function(){
        var userIds = "";
        $("#msg_table").find("input[type=checkbox]:checked").each(function(){
            userIds += userIds == '' ? $(this).val() : "," + $(this).val();
        });
        return userIds;
    };

    _this.ajax = function(url, param, type) {
        var result;
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : param,
            success : function(data) {
                result = data;
            }
        });
        return result;
    };

    _this.initEvent = function() {
        $('#click_msg_type').on("click", "span", function(){
            $('#click_msg_type').find("span").removeClass("active");
            $(this).addClass("active");
            var thisValue = $(this).attr("data-value");
            var oldValue = $("#type").val();
            if(thisValue == oldValue){
                return;
            }
            $("#type").val(thisValue);

            $("#clearSelAll").trigger("click")
            _this.initTable();
        });

        $("#search").click(function () {
            _this.initTable();
        })

        $("#searchAll").click(function () {
            $("#msgTitle").val("");
            _this.initTable();
        })



    }
}
var msg = new msg();